#include "register.h"
#include "ElaPushButton.h"
#include <QDir>
#include <QString>
#include <QFile>
#include <QMessageBox>
#include <QLabel>
#include <QTextStream>

Register::Register(QWidget *parent)
    : QWidget{parent}
{
    setWindowModality(Qt::ApplicationModal);
    setWindowTitle("注册");
    setFixedSize(400,300);
    QLabel *text0 = new QLabel(this);
    QLabel *text1 = new QLabel(this);
    QLabel *text2 = new QLabel(this);
    QLabel *text3 = new QLabel(this);
    QLabel *text4 = new QLabel(this);
    //QLabel *text4 = new QLabel(this);
    text0->setText("用户名");
    text1->setText("账号");
    text2->setText("密码");
    text3->setText("再次输入密码");
    text4->setText("身份凭证");
    text0->move(105,45);
    text1->move(120,85);
    text2->move(120,125);
    text3->move(70,165);
    text4->move(70,205);

    ElaPushButton *Rbtn = new ElaPushButton("注册",this);
    Rbtn->move(175,260);
    //QPushButton *Rbtn = new QPushButton("注册",this);
    //Rbtn->move(120,250);

    RLEdit0->setParent(this);
    RLEdit1->setParent(this);
    RLEdit2->setParent(this);
    RLEdit3->setParent(this);
    RLEdit4->setParent(this);
    RLEdit2->setEchoMode(QLineEdit::Password);
    RLEdit3->setEchoMode(QLineEdit::Password);
    RLEdit4->setEchoMode(QLineEdit::Password);
    RLEdit0->move(160,45);
    RLEdit1->move(160,85);
    RLEdit2->move(160,125);
    RLEdit3->move(160,165);
    RLEdit4->move(160,205);


    connect(Rbtn,&ElaPushButton::clicked,this,&Register::ZhuCe);


}
void Register::ZhuCe()
{
    name = RLEdit0->text();
    Account = RLEdit1->text();
    Password = RLEdit2->text();
    APassword = RLEdit3->text();
    Identify = RLEdit4->text();
    parent = this;
    Parameters(name, Account, Password,APassword,Identify,parent);
}
void Register::Parameters(QString name,QString Account,QString Password,QString APassword ,QString Identify ,QWidget *parent)
{
    flag = 0;
    if(name == "")
    {
         QMessageBox::critical(parent,"","请输入用户名");
    }
    else if(Account == "")
    {
        QMessageBox::critical(parent,"","请输入账号");
    }
    else if(Password == "")
    {
        QMessageBox::critical(parent,"","请输入密码");
    }
    else if(APassword == "")
    {
        QMessageBox::critical(parent,"","请再次输入密码");
    }
    else if(Password != APassword)
    {
        QMessageBox::critical(parent,"","两次输入密码不一致");
    }
    else if(Identify != "9632533")
    {
        QMessageBox::critical(parent,"","身份识别不正确");
    }
    else
    {
        int n = Account.length();
        if(n<6)
        QMessageBox::critical(parent,"","账号长度不小于6位");
        int m = Password.length();
        if(m<6)
        QMessageBox::critical(parent,"","密码长度不小于6位");
        if((m>=6)&&(n>=6))
        {
            QString fileName= filePath + "/Name.txt";
            QFile f(fileName);
            QTextStream in(&f);
            f.open(QIODevice::ReadOnly);
            while(!in.atEnd())
            {
                QString line = in.readLine();

                if(name.toUtf8() == line.toUtf8())
                {
                    flag = 1;
                    QMessageBox::critical(parent,"","用户名重复，请更换一个！");
                    break;
                }
            }
            f.close();
            fileName  = filePath + "/Account.txt";
            f.setFileName(fileName);
            f.open(QIODevice::ReadOnly);
            while(!in.atEnd())
            {
                QString line = in.readLine();

                if(Account.toUtf8() == line.toUtf8())
                {
                    flag = 1;
                    QMessageBox::critical(parent,"","账号重复，请更换一个！");
                    break;
                }
            }
            f.close();
            if(flag == 0)
            {
                if (!ensureDirExists(filePath))
                {
                }
                QString fileName = filePath+"/Name.txt";
                f.setFileName(fileName);
                f.open(QIODevice::WriteOnly|QIODevice::Append);
                f.write(name.toUtf8()+'\n');
                f.close();
                fileName = filePath+"/Account.txt";
                f.setFileName(fileName);
                f.open(QIODevice::WriteOnly|QIODevice::Append);
                f.write(Account.toUtf8()+'\n');
                f.close();
                fileName = filePath+"/Password.txt";
                f.setFileName(fileName);
                f.open(QIODevice::WriteOnly|QIODevice::Append);
                f.write(Password.toUtf8()+'\n');
                f.close();
                QMessageBox::information(this,"","注册成功");
                RLEdit0->clear();
                RLEdit1->clear();
                RLEdit2->clear();
                RLEdit3->clear();
            }

        }
    }

}

bool Register::ensureDirExists(const QString &dirPath) {
    QDir dir(dirPath);
    if (!dir.exists()) {
        if (dir.mkpath(".")) {  // mkpath 可以递归创建目录
            return true;
        } else {
            return false;
        }
    }
    return true;
}

Register::~Register() {}
